<?php

class Designs_IndexController extends App_Controller_Action { 
/**
     * Отображает главную страницу
     */
    public function indexAction() 
    {
        $page = $this->_getParam('page');
        if (!$page) $page = 1;
    	$modelDesigns = new Designs();
    	$designs = $modelDesigns->getDesigns();
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($designs));
        $paginator->setCurrentPageNumber($page);
        $this->view->paginator = $paginator;
    }

    /**
     * Страница из меню
     */    
    public function viewAction() 
    {
        $designId = $this->_getParam('designId');
        $modelDesigns = new Designs();
        $design = $modelDesigns->getDesign($designId);
        $this->view->design = $design;
    }

    public function adminlistAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$modelDesigns = new Designs();
    	$designs = $modelDesigns->getDesigns();
        $this->view->designs = $designs;
    }
    
    public function adminformAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	if ($this->_request->isPost()) {
    		$formDesign = new FormDesigns('');
    		if ($formDesign->isValid($this->_getAllParams())) {
    			$post = $this->_request->getPost();
    			$design = new Designs();
    			
    			$design->setDataFromArray($post);
    			if ($design->id) {
	    			$design->update();
	    		} else {
    				$design->insert();
	    		}
	    		$uploadedData = $formDesign->getValues();
	    		if ($formDesign->fname->getFileName()) {
	    			$design->photo->delPhotoesByRefid();
    				$design->photo->addFile($formDesign->fname);
	    		}
    			$this->_helper->redirector->gotoRoute(array(), 'adminDesigns');
    		}
			$this->view->formDesigns = $formDesign;
    	} else {
	    	$id = $this->_getParam('designId');
	        $modelDesigns = new Designs();
	        $design = $modelDesigns->getDesign($id);
	        $this->view->design = $design;
			$formDesigns = new FormDesigns($design);
			$this->view->formDesigns = $formDesigns;
    	}
    }


    public function admindeleteAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$id = $this->_getParam('designId');
        $modelDesigns = new Designs();
        $res = $modelDesigns->delete('id='.$id);
		$this->_helper->redirector->gotoRoute(array(), 'adminDesigns');
    }
}